var path = require('path');
var server = require('./app/http');
var fs = require('fs');
var Monster = require("./app/domain/monster");
var Equipment = require("./app/domain/equipment");
var rbtree = require('./app/util/rbtree');
//var serversPath = path.join(__dirname + '/config/servers.json');
//var servers = require(serversPath);
var serverconf = {
    name: "http",
    port: 8001,
    host: "127.0.0.1",
    protocol: "http"
}
var app = {
    getBase: __dirname + '/'
};

//初始化等级信息
global.lvlexp = require('./app/util/lvlexp').lvlup;

//初始化怪物
global.monsterMap = new rbtree();
fs.readdirSync("./app/config/monster").forEach(function(file) {

    if (/^\d+.json$/.test(file)) {
        var queue = [];
        var monsterMap = require('./app/config/monster/' + file);
        for (var m in monsterMap) {
            monsterMap[m] = new Monster(monsterMap[m]);
            monsterMap[m].calcAttri();
            queue.push(m);
        }
        var i = file.indexOf('.js');
        var id = parseInt(file.substring(0, i));
        monsterMap.queue = queue;
        global.monsterMap.put(id, monsterMap);
    }
});

//初始化怪物
global.monsters = new rbtree();
global.monsterMap = {};
fs.readdirSync("./app/config/monster").forEach(function(file) {
    if (/^\d+.json$/.test(file)) {
        var queue = [];
        var monster = require('./app/config/monster/' + file);
        for (var m in monster) {
            monster = new Monster(monster[m]);
            monster.calcAttri();
            global.monsters.put(parseInt(m), monster);
            queue.push(parseInt(m));
        }
        var i = file.indexOf('.js');
        var id = parseInt(file.substring(0, i));
        monsterMap[id] = queue;
    }
});

//初始化道具
global.props = new rbtree();
//初始化装备
global.equipments = new rbtree();
fs.readdirSync("./app/config/equipment").forEach(function(file) {
    if (/^\d+.json$/.test(file)) {
        var equipments = require('./app/config/equipment/' + file);
        for (var e in equipments) {
            global.equipments.put(parseInt(e), equipments[e]);
            global.props.put(parseInt(e), equipments[e]);
        }
    }
});

//初始化副本
global.dungeon = new rbtree();
fs.readdirSync("./app/config/dungeon").forEach(function(file) {
    if (/^\d+.json$/.test(file)) {
        var dungeon = require('./app/config/dungeon/' + file);
        for (var d in dungeon) {
            global.dungeon.put(parseInt(d), dungeon[d]);
        }
    }
});

fs.readdirSync("./app/config/prop").forEach(function(file) {
    if (/^\d+.json$/.test(file)) {
        var props = require('./app/config/prop/' + file);
        for (var p in props) {
            global.props.put(parseInt(p), props[p]);
        }
    }
});
//初始化商店信息
global.merchant = {};
fs.readdirSync("./app/config/merchant").forEach(function(file) {
    if (/^\d+.json$/.test(file)) {
        global.merchant = require('./app/config/merchant/' + file);
    }
});

//初始化对象结构
global.playerTree = new rbtree();


server(app, serverconf);